<script setup lang="ts">
import { useWaAccount } from "./utils/hook";
import { ref, onMounted, onBeforeUnmount } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { deviceDetection } from "@pureadmin/utils";
import EditPen from "@iconify-icons/ep/edit-pen";
import Delete from "@iconify-icons/ep/delete";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";
import { hasPerms } from "@/utils/auth";

defineOptions({
  name: "WhatsAppAccount"
});

const formRef = ref();
const tableRef = ref();

const {
  form,
  loading,
  columns,
  dataList,
  userList,
  pagination,
  selectedNum,
  onSearch,
  resetForm,
  openDialog,
  handleDelete,
  handleEnable,
  handleBatchDelete,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange
} = useWaAccount(tableRef);

onMounted(() => {
  window.addEventListener('keyup', handleEnterKey);
});

onBeforeUnmount(() => {
  window.removeEventListener('keyup', handleEnterKey);
});

const handleEnterKey = (e) => {
  if (e.key === 'Enter'&& formRef.value?.$el?.contains(e.target)) {
    onSearch();
  }
};
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px] overflow-auto"
    >
      <el-form-item label="账号：" prop="wa_account">
        <el-input
          v-model="form.wa_account"
          placeholder="请输入账号"
          clearable
          class="!w-[150px]"
        />
      </el-form-item>

      <el-form-item label="使用者：" prop="admin_user_id">
        <el-select
          v-model="form.admin_user_id"
          placeholder="请选择使用者"
          clearable filterable
          class="!w-[150px]"
        >
          <el-option
            v-for="user in userList"
            :key="user.id"
            :label="user.name"
            :value="user.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="备注：" prop="remark">
        <el-input
          v-model="form.remark"
          placeholder="请输入备注关键词"
          clearable
          class="!w-[150px]"
        />
      </el-form-item>

      <el-form-item label="状态：" prop="status">
        <el-select
          v-model="form.status"
          placeholder="请选择状态"
          clearable
          class="!w-[120px]"
        >
          <el-option label="正常" :value="1" />
          <el-option label="停用" :value="0" />
        </el-select>
      </el-form-item>

      <el-form-item label="应用引流：" prop="is_drainage">
        <el-select
          v-model="form.is_drainage"
          placeholder="请选择"
          clearable
          class="!w-[120px]"
        >
          <el-option label="是" :value="1" />
          <el-option label="否" :value="0" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar title="WhatsApp账号管理" :columns="columns" @refresh="onSearch">
      <template #buttons>
        <el-button
          type="primary"
          v-if="hasPerms(['whatsapp:btn:add_account'])"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog()"
        >
          新增账号
        </el-button>
      </template>

      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          ref="tableRef"
          adaptive
          :adaptiveConfig="{ offsetBottom: 108 }"
          align-whole="center"
          table-layout="auto"
          :loading="loading"
          :size="size"
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="pagination"
          :paginationSmall="size === 'small'"
          :header-cell-style="{
            background: 'var(--el-fill-color-light)',
            color: 'var(--el-text-color-primary)'
          }"
          row-key="id"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              v-if="hasPerms(['whatsapp:btn:edit_account'])"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog('修改', row)"
            >
              修改
            </el-button>
            <el-tooltip content="删除已停用，无用账号请设置为禁用即可" placement="top">
            <el-button
              class="reset-margin"
              v-if="hasPerms(['whatsapp:btn:del_account'])"
              link
              type="danger"
              :size="size"
              :icon="useRenderIcon(Delete)"
              @click="handleDelete(row)"
              disabled
            >
              删除
            </el-button>
          </el-tooltip>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
